package com.yuerlive.rainbow.utils;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.support.v4.app.Fragment;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.GlideBitmapDrawable;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.target.Target;
import com.bumptech.glide.signature.StringSignature;
import com.yuerlive.rainbow.API;
import com.yuerlive.rainbow.R;

import java.io.File;
import java.net.URI;

/**
 * Created by xiaoyi on 2017/5/17.
 */

public class GlideUtil {
    private static final String TAG = "GlideUtil";

    private GlideUtil() {
    }

    public static void load(Activity activity, String url, ImageView imageView) {
        Glide.with(activity).load(filterUrl(url))
                .placeholder(R.drawable.default_img)
                .error(R.drawable.default_img)
                .override(720,720)
                .centerCrop()
                .crossFade()
                .listener(new RequestListener<String, GlideDrawable>() {
                    @Override
                    public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
                        imageView.setImageResource(R.drawable.default_img);
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                        return false;
                    }
                })
                .into(new SimpleTarget<GlideDrawable>() {
                    @Override
                    public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
                        imageView.setImageDrawable(resource);
                    }
                });
    }

    public static void load(Activity activity, String url, ImageView imageView, int resId) {
        Glide.with(activity).load(filterUrl(url))
                .placeholder(resId)
                .override(720,720)
                .listener(new RequestListener<String, GlideDrawable>() {
                    @Override
                    public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
                        imageView.setImageResource(R.drawable.default_img);
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                        return false;
                    }
                })
                .into(new SimpleTarget<GlideDrawable>() {
                    @Override
                    public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
                        imageView.setImageDrawable(resource);
                    }
                });
    }

    public static void load(Context context, String url, ImageView imageView, int resId) {
        Glide.with(context).load(filterUrl(url))
                .placeholder(resId)
                .override(720,720)
                .listener(new RequestListener<String, GlideDrawable>() {
                    @Override
                    public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
                        imageView.setImageResource(R.drawable.default_img);
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                        return false;
                    }
                })
                .into(new SimpleTarget<GlideDrawable>() {
                    @Override
                    public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
                        imageView.setImageDrawable(resource);
                    }
                });
    }

    public static void load(Fragment fragment, String url, ImageView imageView) {
        Glide.with(fragment).load(filterUrl(url))
                .placeholder(R.drawable.default_img)
                .error(R.drawable.default_img)
                .override(720,720)
                .listener(new RequestListener<String, GlideDrawable>() {
                    @Override
                    public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
                        imageView.setImageResource(R.drawable.default_img);
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                        return false;
                    }
                })
                .into(new SimpleTarget<GlideDrawable>() {
                    @Override
                    public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
                        imageView.setImageDrawable(resource);
                    }
                });
    }

    public static void load(Context context, String url, ImageView imageView) {
        Glide.with(context).load(filterUrl(url))
                .placeholder(R.drawable.default_img)
                .error(R.drawable.default_img)
                .override(720,720)
                .listener(new RequestListener<String, GlideDrawable>() {
                    @Override
                    public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
                        imageView.setImageResource(R.drawable.default_img);
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                        return false;
                    }
                })
                .into(new SimpleTarget<GlideDrawable>() {
                    @Override
                    public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
                        imageView.setImageDrawable(resource);
                    }
                });
    }

    public static void load(Fragment fragment, String url, ImageView imageView, int resId) {
        Glide.with(fragment).load(filterUrl(url))
                .placeholder(resId)
                .override(720,720)
                .listener(new RequestListener<String, GlideDrawable>() {
                    @Override
                    public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
                        imageView.setImageResource(R.drawable.default_img);
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                        return false;
                    }
                })
                .into(new SimpleTarget<GlideDrawable>() {
                    @Override
                    public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
                        imageView.setImageDrawable(resource);
                    }
                });
    }

    public static void loadNoCache(Activity activity, String url, ImageView imageView) {
        Glide.with(activity).load(filterUrl(url))
                .placeholder(R.drawable.default_img)
                .signature(new StringSignature(String.valueOf(System.currentTimeMillis())))
                .listener(new RequestListener<String, GlideDrawable>() {
                    @Override
                    public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
                        imageView.setImageResource(R.drawable.default_img);
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                        return false;
                    }
                })
                .into(new SimpleTarget<GlideDrawable>() {
                    @Override
                    public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
                        imageView.setImageDrawable(resource);
                    }
                });
    }

    public static void loadBlur(Activity activity, String url, ImageView imageView) {
        imageView.setDrawingCacheEnabled(true);
        Glide.with(activity).load(filterUrl(url))
                .placeholder(R.color.transparent)
                .override(720,720)
                .listener(new RequestListener<String, GlideDrawable>() {
                    @Override
                    public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
                        imageView.setImageResource(R.color.transparent);
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                        return false;
                    }
                })
                .into(new SimpleTarget<GlideDrawable>() {
                    @Override
                    public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
                       /* imageView.setImageDrawable(resource);
                        Bitmap bitmap = imageView.getDrawingCache();
                        if (bitmap == null)return;
                        int width = bitmap.getWidth();
                        int height = bitmap.getHeight();
                        //这里需要先去缩小bitmap 再进行磨合操作 ， 不然会比较耗内存 考虑到这里应该是结束直播调用，比例暂定320:480
                        int newWidth = 320;
                        int newHeight = 480;
                        float scaleWidth = ((float) newWidth) / width;
                        float scaleHeight = ((float) newHeight) / height;
                        Matrix matrix = new Matrix();
                        matrix.postScale(scaleWidth, scaleHeight);
                        Bitmap blur = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix,
                                true);
                        imageView.setImageBitmap(BitmapUtil.fastblur(blur, 20));*/
                        GlideBitmapDrawable bitmapDrawable = (GlideBitmapDrawable) resource;
                        Bitmap bitmap = bitmapDrawable.getBitmap();
                        imageView.setImageBitmap(BitmapUtil.fastblur(bitmap, 20));
                    }
                });
    }


    private static String filterUrl(String url) {
        if (url == null) return "";
        if (url.startsWith("http://") || url.startsWith("https://")) {
            //L.e(TAG, url);
            return url;
        } else {
            return API.IMAGE_HOST + url;
        }
    }


}
